package com.ruoyi.drinkwater.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.drinkwater.domain.ManualParamVo;
import com.ruoyi.drinkwater.mapper.BaseDrinkSegmentMapper;
import com.ruoyi.drinkwater.service.BaseDrinkSegmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
public class BaseDrinkSegmentServiceImpl implements BaseDrinkSegmentService {

    @Autowired
    private BaseDrinkSegmentMapper baseDrinkSegmentMapper;
    @Override
    public List<Map<String, Object>> selectDrinksourceTypesegment(Map<String, Object> map) {
        if("IsManual".equals(map.get("segmentType"))){
            return baseDrinkSegmentMapper.selectDrinksourceTypesegment(map);
        }else if("IsAuto".equals(map.get("segmentType"))){
            return baseDrinkSegmentMapper.selectAutoDrinksourceTypesegment(map);
        }else{
            map.put("segmentType","All");
            return baseDrinkSegmentMapper.selectDrinksourceTypesegment(map);
        }

    }

    @Override
    public List<Map<Object, Object>> maunalDataManagement(Map map) {
        //查询监测时间
        List<Map<Object, Object>> maunalDataManagementDataTimealllist = baseDrinkSegmentMapper.maunalDataManagementDataTimeall(map);
        //查询监测数据
        List<Map<Object, Object>> maunalDataManagementlist = baseDrinkSegmentMapper.maunalDataManagement(map);
        //源对象条件判断属性名 断面id
        String srcEqualProp = "SegmentID";
        //源对象待设置属性名   断面时间
        String srcSetProp = "SampleDate";
        /*因子id加 _Val 是监测因子数据*/
        long t1 = System.currentTimeMillis();
        //使用Java8自带的stream特性进行转换行转列
        List<Map<Object, Object>> resultList = maunalDataManagementDataTimealllist.stream().map(m1 -> {
            maunalDataManagementlist.stream()  //获取监测值的数据
                    .filter(m2 -> Objects.equals(m2.get(srcEqualProp), m1.get(srcEqualProp)) && Objects.equals(m2.get(srcSetProp), m1.get(srcSetProp)))
                    .forEach(m2 -> {
                        m1.put(m2.get("ItemCode") + "_Val", m2.get("OrginalValue"));//监测因子对应值
                        m1.put("SegmentID", m2.get("SegmentID"));//断面id
                        m1.put("SegmentName", m2.get("SegmentName"));//断面名称
                        m1.put("MonitorID", m2.get("MonitorID"));//
                        //m1.put("ReportID", m2.get("ReportID"));//
                        m1.put("ID", m2.get("valuatedataid"));//主键id
                        m1.put("SampleDate", m2.get("SampleDate"));//监测时间
                        m1.put("DrinkSourceName", m2.get("DrinkSourceName"));//水源地名称
                        m1.put("BussinessCategoryCode", m2.get("BussinessCategoryCode"));//监测值类型
                        m2.clear();
                    });
            return m1;
        }).collect(Collectors.toList());

        return resultList;
    }

    public Integer deleteMaunalSegmentsyd( List<ManualParamVo> vo) {
        int deleteint = 0;
        if(vo!=null&&vo.size()>0){
            List<String> segmentIDlist=new ArrayList<>();
            String beginTime=vo.get(0).getSampleDate();
            //根据开始时间查询当前年份的最新累计时间
            String ksbeginTime = beginTime.substring(0, 7);//月份
            String substringyear = beginTime.substring(0, 4);//年份
            String endTime = DateUtils.dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss");
            //循环取值(获取对应值进行删除)
            for (int i = 0; i < vo.size(); i++) {
                ManualParamVo paramVo = vo.get(i);
                String segmentID = paramVo.getSegmentId();//断面id
                String reportID = paramVo.getReportId();//上报批次ID
                String sampleDate = paramVo.getSampleDate();//监测时间
                String bussinessCategoryCode = paramVo.getBussinessCategoryCode();//监测值编码类型
                //json字符转换，前台传参一直乱码暂时隐藏
                /*JSONObject jsonObject = deleteMaunalSegmentlist.getJSONObject(i);
            Object segmentID = jsonObject.get("SegmentID");//断面id
            Object reportID = jsonObject.get("ReportID");//上报批次ID
            Object sampleDate = jsonObject.get("SampleDate");//监测时间
            Object bussinessCategoryCode = jsonObject.get("BussinessCategoryCode");//监测值编码类型*/
                Map deleteMap = new HashMap();
                deleteMap.put("segmentID", segmentID);
                deleteMap.put("reportID", reportID);
                deleteMap.put("sampleDate", sampleDate);
                deleteMap.put("bussinessCategoryCode", bussinessCategoryCode);
                //监测值进行删除
                int datavale = baseDrinkSegmentMapper.deleteWqDataBasicsmonitordata(deleteMap);
                int datavales = baseDrinkSegmentMapper.deleteWqDataIntegrationmonitordata(deleteMap);
                //断面等级河超标级别删除
                int valuatedata = baseDrinkSegmentMapper.deletewqDataBasicevaluatedata(deleteMap);
                int valuatedatas = baseDrinkSegmentMapper.deletewqDataIntegrationevaluatedata(deleteMap);
                //断面累计数据删除
                if(bussinessCategoryCode!=null&&!"CBN3".equals(bussinessCategoryCode)){
                    deleteMap.put("beginTime",ksbeginTime);
                    deleteMap.put("endTime",endTime);
                    deleteMap.put("YearData",substringyear);
                    int i1 = baseDrinkSegmentMapper.deletebasmonthlycumulativedata(deleteMap);
                    if (i1 !=0&&datavale != 0 && datavales != 0 && valuatedata != 0 && valuatedatas != 0) {
                        deleteint++;
                        boolean contains = segmentIDlist.contains(segmentID);
                        if(contains){}else{
                            segmentIDlist.add(segmentID);
                        }
                    }
                }else {
                    if (datavale != 0 && datavales != 0 && valuatedata != 0 && valuatedatas != 0) {
                        deleteint++;
                    }
                }


            }
        }

        return deleteint;
    }
}
